package com.lvandroid.player.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.lvandroid.play.player.AllRoundPlayer;
import com.lvandroid.play.player.AllRoundPlayerManager;
import com.lvandroid.play.util.LogUtil;
import com.lvandroid.player.R;
import com.lvandroid.player.activity.VideoPlayerActivity;
import com.lvandroid.player.utils.XScreenUtils;

import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static com.lvandroid.player.utils.ScenarioManager.PANEL_HEIGHT;
import static com.lvandroid.player.utils.ScenarioManager.PANEL_WITH;
import static com.lvandroid.player.utils.ScenarioManager.SCREEN_HEIGHT;
import static com.lvandroid.player.utils.ScenarioManager.SCREEN_WITH;

public class MainActivity extends AppCompatActivity {
    private FrameLayout mRootView;
    private List<String> mSource = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        initView();
          timer.start();
    }

    private void initView() {
        mRootView = (FrameLayout)findViewById(R.id.root_view);
    }
    ImageView imageView;
    private void setEmptyView() {
         imageView = new ImageView(this);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
        layoutParams.setMargins(0, 0, 0, 0);
        imageView.setLayoutParams(layoutParams);
        imageView.setImageResource(R.drawable.empty_log);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        mRootView.addView(imageView);
    }
    private CountDownTimer  timer=new CountDownTimer(3000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            setEmptyView();
            LogUtil.i("wyt", "添加图片播放器: " );
        }
        @Override
        public void onFinish() {
            mRootView.removeView(imageView);
            LogUtil.i("wyt", "添加播放器: " );
            addVideoPlayer();
            addImageview();
            addHtml();
        }
    };



    private void addImageview() {
        initData();
        if(null == mSource || mSource.size() < 1) {
            LogUtil.w("wyt", "设备中无图片文件.");
        }
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
        layoutParams.setMargins(0, 0,
                300, 800);
        FrameLayout imgeviewer = new FrameLayout(this);
        imgeviewer.setLayoutParams(layoutParams);
        AllRoundPlayer mPlayer = AllRoundPlayerManager.createPlayer(AllRoundPlayerManager.MEDIA_TYPE_IMAGE);
        mRootView.addView(imgeviewer);
        mPlayer.start(mSource, imgeviewer);
    }
    private void addHtml() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
        layoutParams.setMargins(400, 0,
                0, 800);
        FrameLayout htmler = new FrameLayout(this);
        htmler.setLayoutParams(layoutParams);
        mRootView.addView(htmler);
        AllRoundPlayer player = AllRoundPlayerManager.createPlayer(
                AllRoundPlayerManager.MEDIA_TYPE_HTML);
        player.setDataSource("https://image.baidu.com/");
        player.setDisplay(htmler);
        player.start();
    }
    private void addVideoPlayer() {
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
        float factorWidth = SCREEN_WITH * 1.0f / PANEL_WITH;
        float factorHeight = SCREEN_HEIGHT * 1.0f / PANEL_HEIGHT;
        layoutParams.setMargins(0, 800,
                500, 0);
        SurfaceView mSurface = new SurfaceView(this);
        mSurface.setLayoutParams(layoutParams);
        mRootView.addView(mSurface);
        AllRoundPlayer player = AllRoundPlayerManager.getInstance().createPlayer(AllRoundPlayerManager.MEDIA_TYPE_VIDEO);
        if(!TextUtils.isEmpty(getVideoFile())) {
            player.loopPlay(true);
            List<String> datas = new ArrayList<>();
            datas.add(getVideoFile());
            if(datas.size() > 0) {
                player.start(datas, mSurface);
                player.getPlayItem().save();
            } else {
                LogUtil.i("wyt", "设备中无视频文件.");
            }
        }
    }
    public String getVideoFile() {
        Cursor c = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Video.Media.DATA}
                , null ,null, null);
        if(null != c && c.moveToNext()) {
            String path = c.getString(0);
            LogUtil.i("wyt", "getVideoFile : " + path);
            c.close();
            return path;
        }

        return null;
    }

    private void initData() {
        Cursor c = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Video.Media.DATA}
                , null ,null, null);
        int i = 0;
        while(null != c && c.moveToNext()) {
            String path = c.getString(0);
            mSource.add(path);
            LogUtil.i("wyt", "getImageFile : " + path);
            if(++i == 3) {
                break;
            }
        }
        if(i > 0) {
            c.close();
        }
    }
//    public void doClickVideo(View v) {
//        startActivity(new Intent(this, VideoPlayerActivity.class));
//    }
//
//    public void doClickImage(View v) {
//        startActivity(new Intent(this, ImagePlayerActivity.class));
//    }
//
//    public void doClickAudio(View v) {
//        startActivity(new Intent(this, AudioPlayerActivity.class));
//    }
//
//    public void doClickPDF(View v) {
//        startActivity(new Intent(this, PdfPlayerActivity.class));
//    }
//
//    public void doClickHTML(View v) {
//        startActivity(new Intent(this, HtmlPlayerActivity.class));
//    }

}